package com.admin.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.admin.bean.VisitLogBean;

/**   
 * @Title: VisitLogDao.java
 * @Package com.admin.dao
 * @Description: 
 * @author Jason
 * @date 2010-12-16 上午10:24:06    
 * @version    
 */

@Repository
public class VisitLogDao {

	@Autowired private JdbcTemplate jdbcTemplate;
	
	public int addVisitLog(VisitLogBean log){
		String sql = "insert into visit_log(userId,userName,callUrl,paramters)values(?,?,?,?)";
		return jdbcTemplate.update(sql, new Object[]{log.getUserId(),log.getUserName(),log.getCallUrl(),log.getParamters()});
	}
	
	public int findCount(int userId,String startTime,String endTime){
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from visit_log where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(userId!=0){
			sql.append(" and userId = ?");
			params.add(userId);
		}
		if(startTime!=null&&!"".equals(startTime)){
			sql.append(" and callTime>=?");
			params.add(startTime+" 00:00:00");
		}
		if(endTime!=null&&!"".equals(endTime)){
			sql.append(" and callTime<=?");
			params.add(endTime+" 23:59:59");
		}
		return jdbcTemplate.queryForInt(sql.toString(), params.toArray());
	}
	
	public List<VisitLogBean> findByCondition(int userId,String startTime,String endTime,int start,int pageSize){
		StringBuffer sql = new StringBuffer();
		sql.append("select * from visit_log where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(userId!=0){
			sql.append(" and userId = ?");
			params.add(userId);
		}
		if(startTime!=null&&!"".equals(startTime)){
			sql.append(" and callTime>=?");
			params.add(startTime+" 00:00:00");
		}
		if(endTime!=null&&!"".equals(endTime)){
			sql.append(" and callTime<=?");
			params.add(endTime+" 23:59:59");
		}
		sql.append(" order by callTime desc limit ?,?");
		params.add(start);
		params.add(pageSize);
		return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<VisitLogBean>(VisitLogBean.class), params.toArray());
	}
}
